package cz.jm.web.manhunter.client.common;

import com.google.gwt.core.shared.GWT;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.validator.MatchesFieldValidator;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;


public class Utils {
	
	public static final UtilsConstants constatnts = GWT.create(UtilsConstants.class);
	
	public static RegExpValidator createMailValidator() {
		return createMailValidator(constatnts.notValidMail());
	}
	
	public static RegExpValidator createMailValidator(String errorMessage) {
        RegExpValidator validator = new RegExpValidator();  
        if (errorMessage != null) {
        	validator.setErrorMessage(errorMessage);
        }
        validator.setExpression("^([a-zA-Z0-9_.\\-+])+@(([a-zA-Z0-9\\-])+\\.)+[a-zA-Z0-9]{2,4}$");  
        return validator;
	}
	
	
	public static MatchesFieldValidator createMatchIdenticalValueValidator(String matchedFieldName) {
		return createMatchIdenticalValueValidator(matchedFieldName, constatnts.valuesNotMatch());
	}
	
	public static MatchesFieldValidator createMatchIdenticalValueValidator(String matchedFieldName, String errorMessage) {
        MatchesFieldValidator validator = new MatchesFieldValidator();  
        validator.setOtherField(matchedFieldName);  
        if (errorMessage != null) {
        	validator.setErrorMessage(errorMessage);
        }        
        return validator;
	}
	
	public static StaticValidator createStaticValidator(boolean isValid) {
		return createStaticValidator(isValid, null);
	}
	
	public static StaticValidator createStaticValidator(boolean isValid, String errorMessage) {
        StaticValidator validator = new StaticValidator(isValid);
        if (errorMessage != null) {
        	validator.setErrorMessage(errorMessage);
        }
        return validator;
	}
	
	
	public static void setRequired(FormItem item) {
        item.setRequired(true);
        item.setRequiredMessage(constatnts.required());
	}
}
